package org.aspectsense.middleware;



import org.aspectsense.middleware.interfaces.IServiceWrapper;
import org.aspectsense.middleware.interfaces.UtilityParsingService;
import org.osgi.util.tracker.*;

/**
 * This class is used for wrapping a provided type of service.
 * Through the wrapper we can get information about the service such as 
 * which interface class describes the service, if it is currently available,
 * which provider is currently supplying the service and with what utility.
 * 
 * @author Costas Kakousis
 *
 */
public class ServiceWrapper implements IServiceWrapper {

	private String name;
	private boolean isAvailable;
	private String utilityFunction;
	private ComponentContainer provider;
	
	public ServiceWrapper(String name, ComponentContainer provider, String utilityFunction) {
		this.name = name;
		this.isAvailable = false;
		this.provider = provider;
		this.utilityFunction=utilityFunction;
	}
	

	public double getUtility (){
	
		
		//Ask from the parser to compute the value! and return it 
		//UtilityParsingService parser = (UtilityParsingService) Repository.bundleContext.getServiceReference(org.aspectsense.middleware.interfaces.UtilityParsingService.class.getName());
		//return parser.calculateUtility("");
		//System.out.println( Repository.bundleContext.getServiceReference(org.aspectsense.middleware.interfaces.UtilityParsingService.class.getName()));
		//System.out.println(Repository.bundleContext.getServiceReference(org.aspectsense.middleware.interfaces.UtilityParsingService.class.getName()).getClass().getCanonicalName());
		
		ServiceTracker trk = new ServiceTracker(Repository.bundleContext,UtilityParsingService.class.getName(),null );
		trk.open();
		UtilityParsingService parser = (UtilityParsingService) trk.getService();
		//if(parser!=null)
			//System.out.println(utilityFunction);
		//System.out.println(parser.calculateUtility(utilityFunction));
		return parser.calculateUtility(utilityFunction);

	}
	public String getServiceName(){
		return this.name;
	}
	public void enable(boolean value){
		this.isAvailable = value;
	}
	public boolean isAvailable(){
		return this.isAvailable;
	}

	public ComponentContainer getProvider (){

		return this.provider;
	}
	
	public String getUtilityFunction (){
		return this.utilityFunction;
	}
	
	
	public String getState(){
		if (isAvailable){
			return ("Available");
		}
		else
			return("Not Available");
	}

}
